package com.vlong.town.service.notice;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.vlong.town.entity.notice.NoticeInfo;
import com.vlong.town.mapper.notice.NoticeInfoMapper;

/**
 * 公告信息业务实现类
 * @author Assassin
 */
@Service
public class NoticeInfoService {

    // Fields

    @Autowired
    private NoticeInfoMapper noticeInfoMapper;// 公告信息Mapper接口

    // Methods

    /**
     * 添加公告信息
     * @param townId 村镇Id
     * @param title 公告标题
     * @param content 公告内容
     * @param desc 公告简介
     * @param thumbnail 缩略图
     */
    @Transactional
    public void add(Long townId, String title, String content, String desc, String thumbnail) {
        NoticeInfo noticeInfo = new NoticeInfo();
        noticeInfo.setTownId(townId);
        noticeInfo.setState(1);
        noticeInfo.setStatus(1);
        noticeInfo.setTitle(title);
        noticeInfo.setContent(content);
        noticeInfo.setDesc(desc);
        noticeInfo.setThumbnail(thumbnail);
        Long cuTime = System.currentTimeMillis();
        noticeInfo.setCreateTime(cuTime);
        noticeInfo.setUpdateTime(cuTime);
        this.noticeInfoMapper.insert(noticeInfo);
    }

    /**
     * 逻辑删除公告信息
     * @param noticeId 主键值
     */
    @Transactional
    public void delete(Long noticeId) {
        this.noticeInfoMapper.delete(noticeId);
    }

    /**
     * 修改公告信息
     * @param noticeId 公告Id
     * @param title 公告标题
     * @param content 公告内容
     * @param desc 公告简介
     * @param thumbnail 缩略图
     */
    @Transactional
    public void update(Long noticeId, String title, String content, String desc, String thumbnail) {
        NoticeInfo noticeInfo = new NoticeInfo();
        noticeInfo.setNoticeId(noticeId);
        noticeInfo.setTitle(title);
        noticeInfo.setContent(content);
        noticeInfo.setDesc(desc);
        noticeInfo.setThumbnail(thumbnail);
        noticeInfo.setUpdateTime(System.currentTimeMillis());
        this.noticeInfoMapper.update(noticeInfo);
    }

    /**
     * 主键查询公告信息
     * @param noticeId 公告Id
     * @return
     */
    @Transactional(readOnly = true)
    public NoticeInfo queryById(Long noticeId) {
        return this.noticeInfoMapper.selectById(noticeId);
    }

    /**
     * 查询公告信息
     * @param title 公告标题
     * @param content 公告内容
     * @param desc 公告简介
     * @return List<NoticeInfo>
     */
    @Transactional(readOnly = true)
    public List<NoticeInfo> queryList(String title, String content, String desc) {
        NoticeInfo noticeInfo = new NoticeInfo();
        noticeInfo.setTitle(title);
        noticeInfo.setContent(content);
        noticeInfo.setDesc(desc);
        return this.noticeInfoMapper.selectList(noticeInfo);
    }

}
